package com.kuizii.demo.core.service;

import com.kuizii.base.annotaion.AuditLog;
import com.kuizii.base.common.utils.BeanHelper;
import com.kuizii.base.constant.LogType;
import com.kuizii.base.domain.BaseResponse;
import com.kuizii.base.exception.CustomException;
import com.kuizii.demo.api.SysUserService;
import com.kuizii.demo.core.data.service.SysUserDataService;
import com.kuizii.demo.core.domain.entity.user.SysUserEntity;
import com.kuizii.demo.domain.SysUserDto;
import com.kuizii.demo.domain.user.AddUserRequest;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.time.LocalDateTime;

/**
 * <p>
 * 如果业务微复杂 ,由Service层聚合dataService层以及manager层的数据,提供服务 。小型可不再使用该层
 * <p/>
 *
 * @author nuanyang
 * Email freexiaodong@163.com
 * Created by 2019-06-16 16:32:52
 */

@Api(value = "用户表接口文档")
@RestController
@RequestMapping("/core/sys_user")
public class SysUserServiceImpl implements SysUserService {


    @Autowired
    SysUserDataService sysUserDataService;


    @AuditLog(description = "测试")
    @GetMapping("/test")
    public BaseResponse test() {
        try {
            Thread.sleep(10 );
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return BaseResponse.ok();
    }


    @GetMapping("/testAsync")
    public String testAsync() {

        String s = sysUserDataService.testAsync();


        return "res";
    }

    @Override
    @GetMapping("/getUserById")
    public SysUserDto getUserById(String userId) {
        SysUserEntity entity = sysUserDataService.getById(userId);
        SysUserDto result = BeanHelper.copyProperties(entity, SysUserDto::new);
        return result;
    }


    @Override
    @PostMapping("/addUser")
    public void addUser(@Valid AddUserRequest userDto) {
        if (userDto == null) {
            throw new CustomException("传入用户信息不存在");
        }
        SysUserEntity entity = BeanHelper.copyProperties(userDto, SysUserEntity::new);
        entity.setCreateTime(LocalDateTime.now());
        sysUserDataService.save(entity);

    }
}
